package com.bailiban.aigou.admin.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.admin.common.BaseAction;
import com.bailiban.aigou.admin.entity.Classification;
import com.bailiban.aigou.admin.service.ICommodityclassificationService;
import com.bailiban.aigou.admin.utils.IconfontUtils;
import com.bailiban.aigou.admin.utils.PageModel;

@Controller
@Scope("prototype")
@Namespace("/admin/productType")
@Results({
    @Result(name = "error", location = "/WEB-INF/jsp/aigou/500.jsp"),
	@Result(name = "close", location = "/WEB-INF/jsp/aigou/success.jsp")
})
public class CommodityclassificationAction extends BaseAction{

	/**
	 * 类名：CommodityclassificationAction
	 * 类型：@Controller层  action
	 * 功能描述：商品分类管理 商品分类维护
	 * **/
	private static final long serialVersionUID = 1L;
	@Autowired
	private ICommodityclassificationService CommodityclassificationService;
	
	/**
	 * 查询出分页数据集
	 * @return
	 */
	@Action(value="list",results={@Result(name=SUCCESS,location="/WEB-INF/jsp/aigou/admin/product_type/list.jsp")})
	public String list(){
		System.out.println("商品维护");
		  HttpServletRequest request = ServletActionContext.getRequest();
		  super.setPageNo(request.getParameter("pageNo"));
		  super.setPageSize(request.getParameter("pageSize"));
		  PageModel<Classification> pageModel1=new PageModel<Classification>();
		  pageModel1.setPageNo(super.getPageNo());
		  pageModel1.setPageSize(super.getPageSize());
		  PageModel<Classification> pageModel = CommodityclassificationService.getCommodityclassification(pageModel1);
		  request.setAttribute("productTypePages", pageModel);
		  
		  
		  return SUCCESS;
		
		
	}
	/**
	 * 去添加界面
	 * @return
	 */
	@Action(value="addPage",results={@Result(name=SUCCESS,location="/WEB-INF/jsp/aigou/admin/product_type/add.jsp")})
	public String addPage(){
		
		
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			List<String> iconfonts= IconfontUtils.getIconfonts(request);
			request.setAttribute("iconfonts", iconfonts);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		
		return SUCCESS;
		
	}
	/**
	 * 添加商品分类
	 * @return
	 */
	@Action(value="add")
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("productTypeName");
		String descs = request.getParameter("productTypeDesc");
	    String icon= request.getParameter("productTypeIcon");
	    
		Classification classification=new Classification();
		classification.setName(name);
		classification.setDescs(descs);
		classification.setIcon(icon);
		
		CommodityclassificationService.save(classification);
		
		request.setAttribute("url", "/admin/productType/list");
		return "close";
		
	}
	/**
	 * 跳转到修改页面
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@Action(value="updatePage",results={@Result(name=SUCCESS,location="/WEB-INF/jsp/aigou/admin/product_type/update.jsp")})
	public String updatePage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		
		try {
			List<String> iconfonts = IconfontUtils.getIconfonts(request);
			request.setAttribute("iconfonts", iconfonts);
			
			Classification classification = CommodityclassificationService.getProductTypeById(id);
			request.setAttribute("productType", classification);
		} catch (IOException e) {
			return "error";
		}
		 return SUCCESS;
		
	  }
	/**
	 * update 
	 * 修改执行
	 * @return
	 */
	@Action(value="update")
	public String update(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		String productTypeName = request.getParameter("productTypeName");
		String productTypeDesc = request.getParameter("productTypeDesc");
		String productTypeIcon = request.getParameter("productTypeIcon");
		Classification classification = CommodityclassificationService.getProductTypeById(id);
		
		classification.setName(productTypeName);
		classification.setDescs(productTypeDesc);
		classification.setIcon(productTypeIcon);
		CommodityclassificationService.updateProductType(classification);
		
		request.setAttribute("url", "admin/productType/list");
		return "close";
		
		
	}
	/**
	 * 删除对象
	 * @return
	 */
	@Action(value="delete")
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		CommodityclassificationService.deleteProductTypeById(id);
		request.setAttribute("url", "admin/productType/list");
		return "close";
		
	}
	
}
